<script setup>
import { ref, onMounted, onUnmounted } from 'vue'

const isFullscreen = ref(false)
const videoPlayer = ref(null)
const fullscreenVideo = ref(null)
const isPlaying = ref(false)
const showControls = ref(false)

// 视频源 - 可以替换为您自己的视频
const videoSrc = ref('https://vjs.zencdn.net/v/oceans.mp4')
const videoPoster = ref('https://vjs.zencdn.net/v/oceans.png')

// 自动播放视频
const playVideo = async () => {
  try {
    await videoPlayer.value.play()
    isPlaying.value = true
  } catch (error) {
    console.log('自动播放失败，可能需要用户交互:', error)
    isPlaying.value = false
  }
}

const toggleFullscreen = () => {
  isFullscreen.value = !isFullscreen.value

  if (isFullscreen.value) {
    document.documentElement.style.overflow = 'hidden'
    if (isPlaying.value) {
      fullscreenVideo.value.play()
    }
  } else {
    document.documentElement.style.overflow = ''
    fullscreenVideo.value.pause()
    if (isPlaying.value) {
      videoPlayer.value.play()
    }
  }
}

const closeFullscreen = () => {
  isFullscreen.value = false
  document.documentElement.style.overflow = ''
  fullscreenVideo.value.pause()
  if (isPlaying.value) {
    videoPlayer.value.play()
  }
}

const togglePlay = () => {
  if (isPlaying.value) {
    videoPlayer.value.pause()
    isPlaying.value = false
  } else {
    videoPlayer.value.play()
    isPlaying.value = true
  }
}

// 显示控制按钮
const showControlButtons = () => {
  showControls.value = true
}

// 隐藏控制按钮
const hideControlButtons = () => {
  showControls.value = false
}

// 监听视频结束事件，实现循环播放
const handleVideoEnd = () => {
  videoPlayer.value.currentTime = 0
  videoPlayer.value.play()
}

// 监听ESC键退出全屏
const handleKeydown = (e) => {
  if (e.key === 'Escape' && isFullscreen.value) {
    closeFullscreen()
  }
}

onMounted(() => {
  document.addEventListener('keydown', handleKeydown)

  // 组件挂载后尝试自动播放
  setTimeout(() => {
    playVideo()
  }, 1000)
})

onUnmounted(() => {
  document.removeEventListener('keydown', handleKeydown)
})
</script>

<template>
  <div class="right-top">
    <dv-border-box-9 :color="['#00ffd9', 'rgba(0, 255, 217, 0.3)']">
      <div class="content-wrapper">
        <!-- 视频播放器 -->
        <div class="video-container" @mouseenter="showControlButtons" @mouseleave="hideControlButtons">
          <video ref="videoPlayer" :src="videoSrc" :poster="videoPoster" muted loop @ended="handleVideoEnd"></video>

          <!-- 悬停时显示的控制按钮 -->
          <div class="hover-controls" :class="{ show: showControls }" @mouseenter="showControlButtons"
            @mouseleave="hideControlButtons">
            <button class="control-btn play-btn" @click.stop="togglePlay">
              {{ isPlaying ? '❚❚' : '▶' }}
            </button>
            <button class="control-btn fullscreen-btn" @click.stop="toggleFullscreen">
              ⛶
            </button>
          </div>

          <!-- 自动播放失败时显示的播放按钮 -->
          <div v-if="!isPlaying && !showControls" class="play-overlay" @click.stop="togglePlay">
            <div class="play-icon">▶</div>
          </div>
        </div>
      </div>
    </dv-border-box-9>

    <!-- 全屏遮罩层 -->
    <div v-if="isFullscreen" class="fullscreen-overlay" @click="closeFullscreen">
      <button class="close-btn" @click="closeFullscreen">×</button>
      <div class="fullscreen-video" @click.stop>
        <video ref="fullscreenVideo" :src="videoSrc" :autoplay="isPlaying" controls></video>
      </div>
    </div>
  </div>
</template>

<style scoped>
.right-top {
  width: 100%;
  height: 100%;
  position: relative;
}

.content-wrapper {
  width: 100%;
  height: 100%;
  background: rgba(0, 28, 36, 0.5);
  padding: 16px;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.video-container {
  width: 100%;
  height: 100%;
  position: relative;
  cursor: pointer;
  border-radius: 4px;
  overflow: hidden;
}

.video-container video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

/* 悬停控制按钮 */
.hover-controls {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  gap: 20px;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.hover-controls.show {
  opacity: 1;
  visibility: visible;
}

.control-btn {
  width: 50px;
  height: 50px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 255, 217, 0.9);
  color: #0a1a35;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
}

.control-btn:hover {
  background: rgba(0, 255, 217, 1);
  transform: scale(1.1);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}

/* 自动播放失败时的播放按钮 */
.play-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}

.play-icon {
  width: 80px;
  height: 80px;
  background: rgba(0, 255, 217, 0.8);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32px;
  color: #0a1a35;
  transition: all 0.3s ease;
}

.play-overlay:hover .play-icon {
  background: rgba(0, 255, 217, 0.9);
  transform: scale(1.1);
}

/* 全屏样式 */
.fullscreen-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.95);
  z-index: 9999;
  display: flex;
  justify-content: center;
  align-items: center;
}

.fullscreen-video {
  width: 90%;
  height: 80%;
  max-width: 1200px;
}

.fullscreen-video video {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #fff;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 24px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  transition: all 0.3s ease;
  z-index: 10000;
}

.close-btn:hover {
  background: rgba(255, 255, 255, 0.2);
  transform: scale(1.1);
}
</style>